package com.feidian.helper.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.ui.fragment.AcceptFragment;
import com.feidian.helper.ui.fragment.MessageFragment;
import com.feidian.helper.ui.fragment.SendFragment;
import com.feidian.helper.ui.view.MyDialog;
import com.feidian.helper.R;
/**
 * 
 * @author lxq
 * @version 1.0
 * @category 2015.9.17 10:12
 *
 */
public class MainFragmentActivity extends FragmentActivity implements
		OnClickListener {
	private static boolean DEBUG = true;

	private static final String TAG = "MainFragmentActivity";
	private LinearLayout mHomeFragmentTab;//求助贴浏览项目
	private LinearLayout mAcceptTab;//已经接收别人的帮助贴
	private LinearLayout mSendTab;//自己发送的求助帖子
	private LinearLayout mSettingsFragmentTab;//个人用户中心帖子
	
	
	private TextView mHomeFragmentTabTv;
	private TextView mAcceptTabTv;
	private TextView mSendTabTv;
	private TextView mSettingsFragmentTabTv;
	
	private ImageView mHomeFragmentTabIv;
	private ImageView mAcceptTabIv;
	private ImageView mSendTabIv;
	private ImageView mSettingsFragmentTabIv;
	
	private FragmentManager fm;
	private Fragment mHomeFragment,mAcceptFragment, mSendFragment, mSettingsFragment;
	
	private SharedPreferences sp = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_tabs);
		sp = getSharedPreferences(Table.CommentUserInf,Context.MODE_PRIVATE);
		initView();
	}

	private void initView() {
		
		fm = getSupportFragmentManager();
		mHomeFragment = (Fragment) fm.findFragmentById(R.id.ft_main);
		mAcceptFragment = (Fragment) fm.findFragmentById(R.id.ft_accepte);
		mSendFragment = (Fragment) fm.findFragmentById(R.id.ft_send);
		mSettingsFragment = (Fragment) fm.findFragmentById(R.id.ft_settings);

		mHomeFragmentTab = (LinearLayout) findViewById(R.id.btn_home);
		mHomeFragmentTabTv = (TextView)findViewById(R.id.btn_home_tv);
		mHomeFragmentTabIv = (ImageView)findViewById(R.id.btn_home_iv);
		mHomeFragmentTab.setOnClickListener(this);
		
		
		mAcceptTab = (LinearLayout) findViewById(R.id.btn_service);
		mAcceptTabTv = (TextView) findViewById(R.id.btn_service_tv);
		mAcceptTabIv = (ImageView) findViewById(R.id.btn_service_iv);
		mAcceptTab.setOnClickListener(this);
		
		mSendTab = (LinearLayout) findViewById(R.id.btn_explore);
		mSendTabTv = (TextView)findViewById(R.id.btn_explore_tv);
		mSendTabIv = (ImageView)findViewById(R.id.btn_explore_iv);
		mSendTab.setOnClickListener(this);
		
		mSettingsFragmentTab = (LinearLayout) findViewById(R.id.btn_settting);
		mSettingsFragmentTabTv = (TextView)findViewById(R.id.btn_settting_tv);
		mSettingsFragmentTabIv = (ImageView) findViewById(R.id.btn_settting_iv);
		mSettingsFragmentTab.setOnClickListener(this);
		FragmentTransaction ft = fm.beginTransaction();
//		ft.setCustomAnimations(android.R.animator.fade_in,
//				android.R.animator.fade_out);
		ft.show(mHomeFragment);
		ft.hide(mAcceptFragment);
		ft.hide(mSendFragment);
		ft.hide(mSettingsFragment);
		ft.commit();
		/*
		if(AcceptFragment.is_no_news){
			
		}
		*/
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.btn_home: {
			mHomeFragmentTabIv.setImageResource(R.drawable.sur_y);
			mHomeFragmentTabTv.setTextColor(getResources().getColor(R.color.blue));
			mAcceptTabIv.setImageResource(R.drawable.kee_b);
			mAcceptTabTv.setTextColor( getResources().getColor(R.color.textcolor_black));
			mSendTabIv.setImageResource(R.drawable.fou_b);
			mSendTabTv.setTextColor( getResources().getColor(R.color.textcolor_black));
			mSettingsFragmentTabIv.setImageResource(R.drawable.ind_b);
			mSettingsFragmentTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			FragmentTransaction ft = fm.beginTransaction();
//			ft.setCustomAnimations(android.R.animator.fade_in,
//					android.R.animator.fade_out);
			ft.show(mHomeFragment);
			ft.hide(mAcceptFragment);
			ft.hide(mSendFragment);
			ft.hide(mSettingsFragment);
			ft.commit();
			MessageFragment messageFragment = (MessageFragment)mHomeFragment;
			messageFragment.onDownPullRefresh();
			break;
		}

		case R.id.btn_service: {
			mHomeFragmentTabIv.setImageResource(R.drawable.sur_b);
			mHomeFragmentTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mAcceptTabIv.setImageResource(R.drawable.kee_y);
			mAcceptTabTv.setTextColor( getResources().getColor(R.color.blue));
			mSendTabIv.setImageResource(R.drawable.fou_b);
			mSendTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mSettingsFragmentTabIv.setImageResource(R.drawable.ind_b);
			mSettingsFragmentTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			FragmentTransaction ft = fm.beginTransaction();
			ft.hide(mHomeFragment);
			ft.show(mAcceptFragment);
			ft.hide(mSendFragment);
			ft.hide(mSettingsFragment);
			ft.commit();
			AcceptFragment acceptFragment = (AcceptFragment)mAcceptFragment;
			acceptFragment.onDownPullRefresh();
			break;
		}

		case R.id.btn_explore: {
			mHomeFragmentTabIv.setImageResource(R.drawable.sur_b);
			mHomeFragmentTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mAcceptTabIv.setImageResource(R.drawable.kee_b);
			mAcceptTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mSendTabIv.setImageResource(R.drawable.fou_y);
			mSendTabTv.setTextColor(getResources().getColor(R.color.blue));
			mSettingsFragmentTabIv.setImageResource(R.drawable.ind_b);
			mSettingsFragmentTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			FragmentTransaction ft = fm.beginTransaction();
//			ft.setCustomAnimations(android.R.animator.fade_in,
//					android.R.animator.fade_out);
			ft.hide(mHomeFragment);
			ft.hide(mAcceptFragment);
			ft.show(mSendFragment);
			ft.hide(mSettingsFragment);
			ft.commit();
			SendFragment sendFragment = (SendFragment)mSendFragment;
			sendFragment.onDownPullRefresh();
			break;
		}

		case R.id.btn_settting: {
			mHomeFragmentTabIv.setImageResource(R.drawable.sur_b);
			mHomeFragmentTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mAcceptTabIv.setImageResource(R.drawable.kee_b);
			mAcceptTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mSendTabIv.setImageResource(R.drawable.fou_b);
			mSendTabTv.setTextColor(getResources().getColor(R.color.textcolor_black));
			mSettingsFragmentTabIv.setImageResource(R.drawable.ind_y);
			mSettingsFragmentTabTv.setTextColor(getResources().getColor(R.color.blue));
			FragmentTransaction ft = fm.beginTransaction();
//			ft.setCustomAnimations(android.R.animator.fade_in,
//					android.R.animator.fade_out);
			ft.hide(mHomeFragment);
			ft.hide(mSendFragment);
			ft.hide(mAcceptFragment);
			ft.show(mSettingsFragment);
			ft.commit();
			break;
		}

		default:
			throw new RuntimeException("the id not defined!");
		}
	}
	
	  @Override  
	    public boolean onKeyDown(int keyCode, KeyEvent event)  
	    {  
	        if (keyCode == KeyEvent.KEYCODE_BACK )  
	        {  
	        	View view = getLayoutInflater().inflate(R.layout.dialog,null);
	        	final MyDialog dialog = new MyDialog(this,view,R.style.dialog);
	        	Button btn1,btn2;
	        	btn1 = (Button)view.findViewById(R.id.button1);
	        	btn2 = (Button)view.findViewById(R.id.button2);
	        	btn1.setOnClickListener(new OnClickListener(){
	        		@Override
	        		public void onClick(View v){
	    				sp.edit().putString(Table.password, "").commit();
	    				User.password = "null";
	    				//User.isLogin = false;
	    				sp.edit().putBoolean("isLogin", false).commit();
	        			finish();
	        		}
	        	});
	        	btn2.setOnClickListener(new OnClickListener(){
	        		@Override
	        		public void onClick(View v){
	        			dialog.dismiss();
	        		}
	        	});
	            dialog.show();
	        }  
	          
	        return false;  
	          
	    }  
	    /**监听对话框里面的button点击事件*/  
	    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()  
	    {  
	        public void onClick(DialogInterface dialog, int which)  
	        {  
	            switch (which)  
	            {  
	            case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序  
	                finish();  
	                break;  
	            case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框  
	                break;  
	            default:  
	                break;  
	            }  
	        }  
	    };    
}
